<!--
 * @Descripttion: 数据集数据编辑与新增dialog
 * @version: 
 * @Author: 淏
 * @Date: 2023-11-24 13:09:04
 * @LastEditors: 淏
 * @LastEditTime: 2024-04-30 18:11:00
-->
<template>
  <!-- 新增与编辑dialog -->
  <el-dialog
    :visible.sync="dialogVisible"
    :before-close="dialogClose"
    destroy-on-close
    width="50%"
  >
    <div slot="title" class="dialog-footer">
      <span class="text">{{ dialogTitle }}</span>
    </div>
    <el-form style="margin-top: 16px">
      <el-row>
        <div v-for="(item, index) in columns" :key="index">
          <el-col :span="8">
            <el-form-item label-width="150px">
              <span slot="label">
                <span class="text">{{ item.columnCnName }}</span>
              </span>
              <el-input
                size="mini"
                v-model="chooseData[item.columnEnName]"
              ></el-input>
            </el-form-item>
          </el-col>
        </div>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button
        size="mini"
        class="button"
        plain
        @click="dialogVisible = false"
      >
        取 消
      </el-button>
      <el-button
        size="mini"
        class="button"
        type="primary"
        plain
        @click="handleDialog"
      >
        确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { insert_ImportData, update_ImportData } from '@/api/index.js'
  export default {
    // 组件名称
    name: 'insertImport',
    // 组件参数 接收来自父组件的数据
    props: {
      chooseImport: {
        type: Object,
        required: true,
      },
    },
    // 局部注册的组件
    components: {},
    // 组件状态值
    data() {
      return {
        dialogTitle: '',
        dialogType: '',
        columns: '',
        chooseData: '',
        dialogVisible: false,
      }
    },
    // 计算属性
    computed: {},
    // 侦听器
    watch: {},
    // 组件方法
    methods: {
      showDialog(type, columns, chooseData) {
        this.dialogVisible = true
        if (type == 'update') {
          this.columns = columns
          this.chooseData = chooseData
          this.dialogTitle = '编辑数据'
          this.dialogType = 'update'
        } else {
          this.columns = columns
          this.chooseData = chooseData
          this.dialogTitle = '新增数据'
          this.dialogType = 'add'
        }
      },
      dialogClose(done) {
        this.$confirm('确认关闭？')
          .then((_) => {
            done()
            this.clearDialog()
          })
          .catch((_) => {})
      },
      clearDialog() {
        this.dialogVisible = false
        this.chooseData = {}
        this.columns = []
      },
      handleDialog() {
        if (this.dialogType == 'add') {
          this.addImportData()
        } else {
          this.updateImportData()
        }
      },
      addImportData() {
        let importId = this.chooseImport.id
        /*  let columnIds = this.columns.map((item) => {
          return item.id
        })
        columnIds = JSON.stringify(columnIds) */
        let row = JSON.stringify(this.chooseData)
        var qs = require('qs')
        var data = qs.stringify({
          importId: importId,
          row: row,
        })
        insert_ImportData(data).then((res) => {
          /*  loading.close() */
          console.log(res)
          this.$message({
            type: 'success',
            message: res.message,
          })
          this.clearDialog()
        })
      },

      updateImportData() {
        let importId = this.chooseImport.id
        let id = this.chooseData.id
        /*   let columnIds = this.columns.map((item) => {
          return item.id
        })
        columnIds = JSON.stringify(columnIds) */
        let row = JSON.stringify(this.chooseData)
        var qs = require('qs')
        var data = qs.stringify({
          importId: importId,
          id: id,
          importData: row,
        })
        update_ImportData(data).then((res) => {
          /*  loading.close() */
          console.log(res)
          this.$message({
            type: 'success',
            message: res.message,
          })
          this.clearDialog()
        })
      },
    },
    created() {},
    mounted() {},
  }
</script>

<style scoped>
  .text {
    color: #00ffff;
    font-weight: 500;
  }
  .button {
    background: rgba(0, 0, 255, 0.2);
    border-radius: 10px;
    border: 1px solid #295a85;
    color: #00ffff;
    text-shadow: 0 0 25px #00ffff;
    font-weight: 700;
  }
</style>
<style lang="less" scoped>
  /deep/.el-dialog {
    border-radius: 5px;
    background: rgba(48, 89, 224, 0.8);
  }
  /deep/.el-dialog__headerbtn .el-dialog__close {
    color: #ffffff;
  }
</style>
